package com.cn.sl.server.api.system.user.controller

import cn.dev33.satoken.annotation.SaCheckLogin
import cn.dev33.satoken.stp.StpUtil
import com.cn.sl.server.api.system.log.aspect.annotation.WriteLog
import com.cn.sl.server.api.system.menu.model.dto.MenuDto
import com.cn.sl.server.api.system.user.model.dto.AdminUserInfoDto
import com.cn.sl.server.api.system.user.model.dto.CheckPasswordParams
import com.cn.sl.server.api.system.user.model.dto.LoginParam
import com.cn.sl.server.api.system.user.model.dto.TokenDto
import com.cn.sl.server.api.system.user.model.dto.UpdateLoginUserInfoParams
import com.cn.sl.server.api.system.user.model.dto.UpdatePasswordParam
import com.cn.sl.server.api.system.user.service.AdminUserService
import com.cn.sl.server.api.system.user.service.LoginService
import com.cn.sl.server.common.constant.Constant
import com.cn.sl.server.common.dto.SingleValue
import com.cn.sl.server.util.ResponseMessageDto
import com.cn.sl.server.util.ok
import com.cn.sl.server.util.requestSuccessMsg
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import org.springframework.http.ResponseEntity
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

/**
 * 登录相关
 *
 * @author 阮雪峰
 */
@Tag(name = "登录相关")
@RestController
@RequestMapping(Constant.ADMIN_ROOT)
class LoginController(
    val loginService: LoginService,
    val adminUserService: AdminUserService
) {

    /**
     * 登录
     *
     * @param param 用户
     * @return org.springframework.http.CustomResponseEntity
     */
    @Operation(summary = "登录")
    @WriteLog("登录")
    @PostMapping("/login")
    fun login(
        @Validated @RequestBody param: LoginParam,
    ): ResponseEntity<TokenDto> {
        return ok(loginService.login(param.toDto()))
    }

    /**
     * 获取用户信息
     *
     * @return ResponseEntity
     */
    @Operation(summary = "获取用户信息")
    @SaCheckLogin
    @GetMapping("/user-info")
    fun info(): ResponseEntity<AdminUserInfoDto> {
        return ok(loginService.info())
    }

    /**
     * 退出登录
     *
     * @return org.springframework.http.ResponseEntity
     */
    @Operation(summary = "退出登录")
    @SaCheckLogin
    @PostMapping("/logout")
    fun logout(): ResponseEntity<ResponseMessageDto> {
        loginService.logout()
        return requestSuccessMsg()
    }

    /**
     * 验证密码
     */
    @Operation(summary = "验证密码")
    @SaCheckLogin
    @PostMapping("/check-password")
    fun checkPassword(@RequestBody param: CheckPasswordParams): ResponseEntity<SingleValue<Boolean>> {
        return ok(SingleValue(loginService.checkPassword(param.toDto())))
    }

    /**
     * 修改登录用户信息
     */
    @Operation(summary = "修改登录用户信息")
    @SaCheckLogin
    @PostMapping("/update-user-info")
    fun updateUserInfo(@RequestBody param: UpdateLoginUserInfoParams): ResponseEntity<ResponseMessageDto> {
        loginService.updateUserInfo(param.toDto())
        return requestSuccessMsg()
    }

    /**
     * 修改密码
     */
    @Operation(summary = "修改密码")
    @SaCheckLogin
    @PostMapping("/update-password")
    fun updatePassword(@RequestBody @Validated param: UpdatePasswordParam): ResponseEntity<ResponseMessageDto> {
        loginService.updatePassword(param.toDto())
        return requestSuccessMsg()
    }

    /**
     * 获取用户菜单
     *
     * @return ResponseEntity
     */
    @Operation(summary = "获取用户菜单")
    @SaCheckLogin
    @GetMapping("/login/user/menu-list")
    fun menuList(): ResponseEntity<List<MenuDto>> {
        return ok(adminUserService.findMenu(StpUtil.getLoginIdAsLong()))
    }
}